package com.star.service.impl;

import com.star.NotFoundException;
import com.star.dao.TagMapper;
import com.star.dao.TypeMapper;
import com.star.pojo.Tag;
import com.star.pojo.Type;
import com.star.service.TagService;
import com.star.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * com.star.service
 *
 * @Description: 标签接口实现类
 * @Date: 2020-08-06 15:54
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Transactional//实现事务操作
    @Override
    public int save(Tag tag) {
        return tagMapper.save(tag);
    }

    @Transactional
    @Override
    public Tag getTag(Long id) {
        Tag tag = tagMapper.getTag(id);
        return tag;
    }

    @Override
    public Tag getTagByName(String name) {
        return tagMapper.getTagByName(name);
    }

    @Transactional
    @Override
    public List<Tag> getAllTag() {
        List<Tag> allTag = tagMapper.getAllTag();
        return allTag;
    }

    @Transactional
    @Override
    public int updateTag(Long id, Tag tag) {
        Tag t = tagMapper.getTag(id);
        if (t == null) {
            throw new NotFoundException("不存在该标签！");
        }
        BeanUtils.copyProperties(tag, t);
        return tagMapper.updateTag(t);
    }

    @Transactional
    @Override
    public int deleteTag(Long id) {
        return tagMapper.deleteTag(id);
    }

    @Override
    public List<Tag> getAllTagAndBlog() {
        List<Tag> allTagAndBlog = tagMapper.getAllTagAndBlog();
        return allTagAndBlog;
    }
}
